package jwanalytics.agentbasedsimulations;

import java.awt.Color;

import javax.swing.JFrame;

import jwanalytics.agentbasedsimulations.BuiltInSimulation.SimulationWebPage;

import sim.display.Console;
import sim.display.Controller;
import sim.display.Display2D;
import sim.display.GUIState;
import sim.engine.SimState;
import sim.portrayal.grid.FastValueGridPortrayal2D;

/** This is largely based on Tutorial1 and Tutorial2 from Mason documentation. 
 * If you run this class the simulation seen will be colored dots for agents
 * in state one and black dots for agents on state 0. */
public class BuiltInSimulationGUI extends GUIState {

	    public Display2D display;
	    
	    public JFrame displayFrame;
	    
	    /** This is where the statis numeric values are set and painted. */
	    FastValueGridPortrayal2D gridPortrayal = new FastValueGridPortrayal2D();
	    
	    public BuiltInSimulationGUI(String protocolAndHost) { super(new BuiltInSimulation(System.currentTimeMillis(), protocolAndHost)); }
	    
	    public BuiltInSimulationGUI(SimState state) { super(state); }

	    public static String getName() { return "JWAnaltics built in simulation."; }
	    
	    /** This is shown on the user interface of the simulation. ON a particular pane. */
	    public static Object getInfo() {
	        return 
	        "<H2>JWAnalytics built in simulation</H2>" +
	        "<p>Web sit visitors are modeled as agents and presented as dots on the grid.</p> "+
	        "<p>The colour of the dot is the background colour of the page the are now (black being no page) " +
	        "See: http://code.google.com/p/jwanalytics/wiki/Optimisation for the sitemap." +
	        "See: http://localhost:8080/jwanalyticsWeb/faces/site1.jsp for the homepage of the site.</p>";
	    }    
		
	    public void setupPortrayals() {
	    	// tell the portrayals what to portray and how to portray them
	    	gridPortrayal.setField(((BuiltInSimulation)state).grid);
	    	Color[] palette = new Color[8];
	    	// This matches the colours on sitemap present on: http://code.google.com/p/jwanalytics/wiki/Optimisation
	    	palette[SimulationWebPage.none.ordinal()] = Color.black;  //created did not visit the site yet.  Still have to make-up his mind on whether it will visit and when.
	    	palette[SimulationWebPage.site1.ordinal()] = Color.blue;   //site1.jsp page.	    	
	    	palette[SimulationWebPage.activities.ordinal()] = Color.green;  //activities.jsp page (looking at the activities our business develops, but not purchasing anything).
	    	palette[SimulationWebPage.page1.ordinal()] = Color.yellow; //almost purchasing, one of the optimized pages page1.jsp, page2.jsp, page3.jsp
	    	palette[SimulationWebPage.page2.ordinal()] = Color.yellow; //almost purchasing, one of the optimized pages page1.jsp, page2.jsp, page3.jsp	    	
	    	palette[SimulationWebPage.page3.ordinal()] = Color.yellow; //almost purchasing, one of the optimized pages page1.jsp, page2.jsp, page3.jsp	    		    	
	    	palette[SimulationWebPage.goal.ordinal()] = Color.orange; //achieved the goal page.  Will stay here for 2 iterations and then back to state zero, pallete colour 0 for none.
	    	gridPortrayal.setMap(new sim.util.gui.SimpleColorMap(palette));
	    	
	    	display.reset();    // reschedule the displayer
	    	
	    	display.repaint();  // redraw the display
	    }
	    
	    
	    public void load(SimState state)  {
	    	super.load(state);
	    	//we now have new grids.  Set up the portrayals to reflect that
	    	setupPortrayals();
        }
	    
	    
	    public void start() {
	    	super.start();      
	    	setupPortrayals();  // set up our portrayals
	    }
	    
	    
	    public void init(Controller c) {
	    	super.init(c);
	    
	    	// Make the Display2D.  We'll have it display stuff later.
	    	BuiltInSimulation builtInSimulation = (BuiltInSimulation)state;
	    	display = new Display2D(builtInSimulation.gridWidth * 5, builtInSimulation.gridHeight * 5,this, 1);
	    	displayFrame = display.createFrame();
	    	c.registerFrame(displayFrame);   // register the frame so it appears in the "Display" list
	    	displayFrame.setVisible(true);

	    	// specify the backdrop color  -- what gets painted behind the displays
	    	display.setBackdrop(Color.black);
	    	
	    	display.attach(gridPortrayal,"JWAnalytics simulation");  // attach the portrayals	    	
	    }
	    
	    
	    /** THis method is exactly the same as the AntsForageWithUI equivalent (AntsForageWithUI simulation from mason demo simulations). */
	    public static void main(String[] args) {
	    	String protocolAndHost = "http://localhost:8080";
	    	if (args.length > 0) {
	    		protocolAndHost = args[0];
	    	}
	    	
	    	BuiltInSimulationGUI builtInSimulationGUI = new BuiltInSimulationGUI(protocolAndHost);
	    	Console c = new Console(builtInSimulationGUI);
	    	c.setVisible(true);
	    }
	    
	    
	    /** Copied from AntsForageWithUI. */
	    public void quit() {
	    	super.quit();
	    	
	    	// disposing the displayFrame automatically calls quit() on the display,
	    	// so we don't need to do so ourselves here.
	    	if (displayFrame!=null) displayFrame.dispose();
	    	displayFrame = null;  // let gc
	    	display = null;       // let gc
        }
        
	    
	}
